home *** CD-ROM | disk | FTP | other *** search
/ 220 Jogos / 220 jogos.iso / tetris / tetron / SOURCE / TOP10.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-14  |  5.6 KB  |  215 lines

  1. //----------------------------------------------------------------------------------------
  2. //----------------------------------------------------------------------------------------
  3. //
  4. //        Filename        :    top10.cpp
  5. //        Description        :    Member Definitions for top10 class
  6. //        Author            :   Marnich van Rensburg (2002)
  7. //
  8. //----------------------------------------------------------------------------------------
  9. //----------------------------------------------------------------------------------------
  10.  
  11. #include "top10.h"
  12.  
  13.  
  14. //----------------------------------------------------------------------------------------
  15. //        Description        :    Constructor
  16. //----------------------------------------------------------------------------------------
  17.  
  18. Top10 :: Top10()                                                // Initialize Our Timer (Get It Ready)
  19. {
  20.     for(int n = 0;n < 10; n++)
  21.     {
  22.         for(int c = 0; c < 23; c++) 
  23.             List[n].Name[c] = '.';
  24.         List[n].Name[23] = '\0';
  25.         List[n].Level = 0;
  26.         List[n].Lines = 0;
  27.         List[n].Score = 0;
  28.     }//for
  29.  
  30. } // Top10
  31.  
  32.  
  33. //----------------------------------------------------------------------------------------
  34. //        Description        :    Init highscore table
  35. //----------------------------------------------------------------------------------------
  36.  
  37. void Top10 :: Init()
  38. {
  39.     // Load Scores from file
  40.     if(!Load())
  41.     {
  42.         // If theres no file asume defaults
  43.         for(int n = 0;n < 10; n++)
  44.         {
  45.             for(int c = 0; c < 23; c++) 
  46.                 List[n].Name[c] = '.';
  47.             List[n].Name[23] = '\0';
  48.             List[n].Level = 0;
  49.             List[n].Lines = 0;
  50.             List[n].Score = 0;
  51.         }//for
  52.         Save();
  53.     }//if
  54.  
  55. }//end Init
  56.  
  57.  
  58.  
  59. //----------------------------------------------------------------------------------------
  60. //        Description        :    Save scores to file
  61. //----------------------------------------------------------------------------------------
  62.  
  63. void Top10 :: Save()
  64. {
  65.     FILE *p_File;
  66.     
  67.     p_File = fopen("data/scores.dat", "w");
  68.     
  69.     for(int n = 0;n < 10; n++)
  70.     {
  71.         fprintf(p_File, "%s", List[n].Name);
  72.         fprintf(p_File, "%c", '\0');
  73.         fprintf(p_File, "%c", ',');
  74.         fprintf(p_File, "%u", List[n].Score);
  75.         fprintf(p_File, "%c", ',');
  76.         fprintf(p_File, "%u", List[n].Lines);
  77.         fprintf(p_File, "%c", ',');
  78.         fprintf(p_File, "%u", List[n].Level);
  79.         fprintf(p_File, "%c", ',');
  80.     }//for
  81.     
  82.     if(p_File)
  83.     {
  84.         fclose(p_File);
  85.         Encrypt("data/scores.dat");    //encrypt the file
  86.     }//if
  87.  
  88. }//end Save
  89.  
  90.  
  91.  
  92.  
  93. //----------------------------------------------------------------------------------------
  94. //        Description        :    Load scores from file
  95. //----------------------------------------------------------------------------------------
  96.  
  97. bool Top10 :: Load()
  98. {
  99.     Encrypt("data/scores.dat");    //decrypt the file
  100.     FILE *p_File = NULL;
  101.     
  102.     p_File = fopen("data/scores.dat", "r");
  103.  
  104.     if(p_File)
  105.     {
  106.         for(int n = 0;n < 10; n++)
  107.             fscanf(p_File, "%24s,%u,%u,%u,", &List[n].Name, &List[n].Score, &List[n].Lines, &List[n].Level);         
  108.     
  109.         fclose(p_File);
  110.  
  111.         Encrypt("data/scores.dat");    //encrypt the file    
  112.         return true;
  113.     }else{
  114.         Encrypt("data/scores.dat");    //encrypt the file
  115.         return false;
  116.     }//if
  117.  
  118. }//end Load
  119.  
  120.  
  121.  
  122. //----------------------------------------------------------------------------------------
  123. //        Description        :    Adds a new entry onto the top10 list
  124. //----------------------------------------------------------------------------------------
  125.  
  126. void Top10 :: SetEntry(char v_Name[25], unsigned int v_Score, unsigned int v_Lines, unsigned int v_Level)
  127. {
  128.     int n = 0;
  129.  
  130.     //Determine where the entry should go (ep = new entry position)
  131.     for(int ep = 0;(ep < 10) && (v_Score < List[ep].Score); ep++)
  132.         ;
  133.     
  134.     // if new intry not at last positon on list
  135.     if(ep != 9)
  136.     {
  137.         //shift entries down from the new entry's position
  138.         for(n = 9; n > ep; n--)
  139.             Copy(List[n - 1], List[n]);    
  140.     }//if
  141.  
  142.     //Insert new entry
  143.     for(n = 0; n < 23; n++) 
  144.         List[ep].Name[n] = v_Name[n];
  145.  
  146.     List[ep].Score = v_Score;
  147.     List[ep].Level = v_Level;
  148.     List[ep].Lines = v_Lines;
  149.     
  150.     // Save the updated highscore table
  151.     Save();
  152.  
  153. }//end SetEntry
  154.  
  155.  
  156.  
  157. //----------------------------------------------------------------------------------------
  158. //        Description        :    Used for checking if the player has a new highscore
  159. //----------------------------------------------------------------------------------------
  160.  
  161. bool Top10 :: IsNewHighScore(unsigned int v_Score)
  162. {
  163.     if(v_Score > List[9].Score)
  164.         return true;
  165.     else
  166.         return false;
  167.  
  168. }//end IsNewHighScore
  169.  
  170.  
  171. //----------------------------------------------------------------------------------------
  172. //        Description        :    Makes a copy of a highscore entry
  173. //----------------------------------------------------------------------------------------
  174.  
  175. void Top10 :: Copy(HighScore& ref_Source, HighScore& ref_Dest)
  176. {
  177.  
  178.     for(int n = 0; n < 23; n++) 
  179.         ref_Dest.Name[n] = ref_Source.Name[n];
  180.  
  181.     ref_Dest.Score = ref_Source.Score;
  182.     ref_Dest.Lines = ref_Source.Lines;
  183.     ref_Dest.Level = ref_Source.Level;
  184.  
  185. }//end Copy
  186.  
  187.  
  188.  
  189. //----------------------------------------------------------------------------------------
  190. //        Description        :    Very elementary encryption for encrypting highscore file
  191. //                            to decrypt just pass encrypted file through the function again
  192. //----------------------------------------------------------------------------------------
  193.  
  194. void Top10 :: Encrypt(const char *TargetFile)
  195. {
  196.     FILE *p_if = NULL;
  197.     FILE *p_of = NULL;
  198.     int c;
  199.     
  200.     p_if = fopen(TargetFile, "r");
  201.     p_of = fopen("data/tmp", "w");
  202.  
  203.     if(p_if && p_of)
  204.     {
  205.         while ((c = fgetc(p_if)) != EOF)
  206.             fputc((unsigned char)c + 128, p_of);
  207.  
  208.         fclose(p_if);
  209.         fclose(p_of);
  210.         
  211.         remove(TargetFile);
  212.         rename("data/tmp", TargetFile);
  213.     }//if
  214.  
  215. }//end Encrypt